package com.yatuhashi.manual.mhp3.parse;

import java.io.InputStream;

import org.xmlpull.v1.XmlPullParser;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Xml;

import com.yatuhashi.manual.mhp3.bean.QuestDetailBean;
import com.yatuhashi.manual.mhp3.db.DataBaseHelper;
import com.yatuhashi.manual.mhp3.util.LogUtil;

public class QuestDetailParser {
    private Context mContext;
    
    public QuestDetailParser(Context context) {
        mContext = context;
    }
    
    public boolean parse(InputStream is) {
        boolean success = false;
        
        QuestDetailBean quest = null;
        int counter = 0;
        
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setInput(is, "UTF-8");
            
            int eventType = parser.getEventType();
            
            while(eventType!=XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    String startTag = parser.getName();
                    if("root".equals(startTag)) {
                        
                    } else if("item".equals(startTag)) {
                       quest = new QuestDetailBean(); 
                       quest.setId(counter);
                    } else if("qid".equals(startTag)) {
                        quest.setQid(Integer.parseInt(parser.nextText()));
                    } else if("target".equals(startTag)) {
                    	quest.setTarget(parser.nextText());
                    } else if("reward".equals(startTag)) {
                    	quest.setReward(parser.nextText());
                    } else if("deposit".equals(startTag)) {
                    	quest.setDeposit(parser.nextText());
                    } else if("monster".equals(startTag)) {
                    	quest.setMonster(parser.nextText());
                    } else if("habitat".equals(startTag)) {
                    	quest.setHabitat(parser.nextText());
                    } else if("limittime".equals(startTag)) {
                    	quest.setLimit(parser.nextText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    String endTag = parser.getName();
                    if("item".equals(endTag)) {
                        registDb(quest);
                        counter++;
                    }
                    break;
                }
                eventType = parser.next();
            }
            success = true;
        } catch (Exception e) {
            LogUtil.logE(e.toString());
            success = false;
        }
        return success;
    }
    
    private boolean registDb(QuestDetailBean quest) {
        DataBaseHelper dbHelper = new DataBaseHelper(mContext);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        String sql = 
            "insert into quest_detail_table (" +
            "qid, " +
            "target, " +
            "reward, " +
            "deposit, " +
            "monster, " +
            "habitat, " +
            "limittime, " +
            "mid " +
            ") values('" +
            quest.getQid() +
            "','" +
            quest.getTarget() +
            "','" +
            quest.getReward() +
            "','" +
            quest.getDeposit() +
            "','" +
            quest.getMonster() +
            "','" +
            quest.getHabitat() +
            "','" +
            quest.getLimit() +
            "','" +
            quest.getQid() +
            "');";
        
        db.execSQL(sql);
        
        return true;
    }
}
